package com.util;

import org.fusesource.jansi.Ansi;
import org.fusesource.jansi.AnsiConsole;

import java.awt.print.PrinterException;
import java.util.Arrays;
import java.util.List;

public class TerminalPrintColorStrUtil {
    public static final Ansi.Color Default_Color = Ansi.Color.WHITE;

    private static Ansi formatStr(String content, Ansi.Color colour) {
        return Ansi.ansi().fg(colour).a(content);
    }

    private static Ansi formatStr(char c, Ansi.Color colour) {
        return Ansi.ansi().fg(colour).a(c);
    }

    /**
     * 根据内容和颜色输出
     *
     * @param content 内容
     * @param color   颜色
     */
    public static void printByLine(String content, Ansi.Color color, int sleepTime) {
        AnsiConsole.systemInstall();
        System.out.println(formatStr(content, color));
        AnsiConsole.systemUninstall();
        ThreadUtil.sleep(sleepTime);
    }

    /**
     * 根据内容和颜色输出
     *
     * @param contentList 内容列表
     * @param colors      颜色列表
     */
    public static void printByLine(List<String> contentList, List<Ansi.Color> colors, int sleepTime) {
        AnsiConsole.systemInstall();
        for (int i = 0; i < contentList.size(); i++) {
            String content = contentList.get(i);
            Ansi.Color color = colors.get(i);
            System.out.println(formatStr(content, color));
        }
        AnsiConsole.systemUninstall();
        ThreadUtil.sleep(sleepTime);
    }

    /**
     * 根据内容输出
     *
     * @param contents  一行的内容
     * @param colors    一行的颜色
     * @param sleepTime 休眠时间
     */
    public static void printByLine(char[] contents, Ansi.Color[] colors, int sleepTime) {
        AnsiConsole.systemInstall();
        if (contents.length == 0) {
            throw new RuntimeException("输入的内容为空");
        }
        if (contents.length < colors.length) {
            throw new RuntimeException("颜色的数量比内容多");
        }
        if (contents.length > colors.length) {
            colors = Arrays.copyOf(colors, contents.length);
        }
        for (int i = 0; i < contents.length; i++) {
            Ansi.Color color = colors[i];
            if (color == null) {
                color = Default_Color;
            }
            System.out.print(formatStr(contents[i], color));
        }
        System.out.println();
        AnsiConsole.systemUninstall();
        ThreadUtil.sleep(sleepTime);
    }

    /**
     * 根据内容输出
     *
     * @param contents  一行的内容
     * @param colors    一行的颜色
     * @param sleepTime 每个字符的间隔休眠时间
     */
    public static void printByChar(char[] contents, Ansi.Color[] colors, int sleepTime) {
        AnsiConsole.systemInstall();
        if (contents.length == 0) {
            throw new RuntimeException("输入的内容为空");
        }
        if (contents.length < colors.length) {
            throw new RuntimeException("颜色的数量比内容多");
        }
        if (contents.length > colors.length) {
            colors = Arrays.copyOf(colors, contents.length);
        }
        for (int i = 0; i < contents.length; i++) {
            Ansi.Color color = colors[i];
            char content = contents[i];
            if (color == null) {
                color = Default_Color;
            }
            if (content == ' ') {
                System.out.print(content);
                continue;
            }
            System.out.print(formatStr(content, color));
            ThreadUtil.sleep(sleepTime);
        }
        System.out.println();
        AnsiConsole.systemUninstall();
    }
}
